﻿using System;
using System.Diagnostics;
using System.Reactive.Linq;
using KinonekoSoftware.CodariaDB.Presets;
using KinonekoSoftware.CodariaDB.Utils;
using KinonekoSoftware.Foundation.Bus;
using KinonekoSoftware.JuXiaoYouDB.Core;
using KinonekoSoftware.Studio.Galleries;
using KinonekoSoftware.Studio.Models.Shells;
using KinonekoSoftware.Studio.ViewModels.Main;
using KinonekoSoftware.UI;
using NLog;

namespace KinonekoSoftware.Studio.ViewModels
{
    public sealed partial class AppViewModel : TabbedShell
    {
        internal readonly HashSet<string> RepositoryHashSet;


        public AppViewModel()
        {
            var runtime = Ioc.Get<RepositoryRuntime>();

            _notifyQueue = new MessageBus<JuXiaoYouNotification>
            {
                FallbackHandler = OnNotificationProc,
            };

            Notifications     = new ObservableCollection<JuXiaoYouNotification>();
            RepositoryHashSet = new HashSet<string>();
            Repositories      = new ObservableCollection<RepositoryCache>();
            LaunchContext = new JuXiaoYouLaunchContext
            {
                Repositories  = Repositories,
                Notifications = Notifications,
            };


            OpenQuickPanelCommand = new ActionCommand(DoOpenQuickPanelCommand);
            GotoSettingCommand    = new ActionCommand(DoGotoSettingCommand);
            GotoBugReportCommand  = new ActionCommand(DoGotoBugReportCommand);
            GotoTutorialCommand   = new ActionCommand(DoGotoTutorialCommand);

            SwitchRepositoryCommand     = new FuncCommand<RepositoryCache>(DoSwitchRepositoryCommand);
            LocateRepositoryCommand     = new ActionCommand(DoLocateRepositoryCommand);
            DeleteRepositoryCommand     = new FuncCommand<RepositoryCache>(DoDeleteRepositoryCommand);
            ExportRepositoryWikiCommand = new ActionCommand(DoExportRepositoryWikiCommand);
            SyncRepositoryCommand       = new ActionCommand(DoSyncRepositoryCommand);

            RemoveAllNotificationCommand = new ActionCommand(DoRemoveAllNotificationCommand);

            runtime.Opening = OnDatabaseOpeningImpl;
            runtime.Closing = OnDatabaseClosingImpl;
        }

        public override bool ShouldDirectExit
        {
            get
            {
                //
                //
                if (Ioc.Get<RepositoryRuntime>().Mode == DataMode.Protected)
                {
                    return false;
                }

                //
                //
                if (Inboard.Any(x => x.EnableSafetyExit  && x.IsChange) ||
                    Outboard.Any(x => x.EnableSafetyExit && x.IsChange))
                {
                    return false;
                }


                return true;
            }
        }


        #region Properties

        /// <summary>
        /// 表示
        /// </summary>
        public bool IsLaunch => LaunchContext.IsLaunch;
        public bool                                        IsLoaded          => LaunchContext.IsLoaded;
        public JuXiaoYouLaunchContext                      LaunchContext     { get; }
        public ObservableCollection<RepositoryCache>       Repositories      { get; }
        public ObservableCollection<JuXiaoYouNotification> Notifications     { get; }
        public RepositoryCache                             CurrentRepository => LaunchContext.CurrentRepository;
        public string                                      RepositoryPath    => CurrentRepository?.Path;

        #endregion
    }
}